package com.tianhui.consignortwofei.mvp.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.AutoCompleteTextView;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.CheckUtil;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseDataListActivity;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.DriverModel;
import com.tianhui.consignortwofei.mvp.model.enty.DriverInfo;
import com.tianhui.consignortwofei.mvp.ui.adapter.DriverDetailAdapter;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.inter.OnItemClickListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2021/8/10
 * @description
 */
public class DriverDetailListActivity extends BaseDataListActivity {

    @BindView(R.id.layout_search_editText)
    AutoCompleteTextView searchEditText;

    private DriverModel driverModel;

    private String mSearchKey;

    @Override
    public int getLayoutId() {
        return R.layout.activity_driver_detail_list;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }


    @Override
    public RecyclerArrayAdapter createAdapter() {
        return new DriverDetailAdapter(getContext());
    }

    private void setResult(DriverInfo driverInfo) {
        Intent intent = new Intent();
        intent.putExtra(KeyValue.KEY_NAME, driverInfo.drivername);
        intent.putExtra(KeyValue.KEY_PHONE, driverInfo.drivertel);
        intent.putExtra(KeyValue.KEY_VEHICLE_NUMBER, driverInfo.defaultvehiclenum);
        setResult(KeyValue.KEY_RESULT_CODE, intent);
        finish();
    }

    @Override
    public void initListener() {
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                DriverInfo driverInfo = (DriverInfo) mAdapter.getAllData().get(position);
                if (driverInfo != null && !TextUtils.isEmpty(driverInfo.drivertel)) {
                    setResult(driverInfo);
                }
            }
        });
    }

    @Override
    public void requestDataList(int page, boolean isDialog) {
        mSearchKey = searchEditText.getText().toString();
        if (checkSearchKey(mSearchKey)) {
            requestDriverList(mSearchKey, isDialog);
        } else {
            stopRefresh();
        }
    }

    @Override
    public void initData() {
        setTitle("司机明细");

        searchEditText.setHint("请输入司机手机号");

    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @OnClick(R.id.layout_search_textView)
    public void searchDriverDetail() {
        mSearchKey = searchEditText.getText().toString();
        if (checkSearchKey(mSearchKey)) {
            setRefresh(true);
            requestDriverList(mSearchKey, true);
        }
    }

    private boolean checkSearchKey(String searchKey) {
        if (TextUtils.isEmpty(searchKey)) {
            showEmptyView();
        } else if (CheckUtil.isMobileNO(mSearchKey)) {
            return true;
        } else {
            showToast("请输入正确的手机号");
        }
        return false;
    }


    public void requestDriverList(String phone, boolean isDialog) {
        Map<String, String> map = new HashMap<>();
        map.put("drivertel", phone);
        if (driverModel == null) {
            driverModel = new DriverModel();
        }
        driverModel.assignDriverListModel(this, map, isDialog, bindLifecycle(), new ObserverResponseListener<CommonResponse<List<DriverInfo>>>() {
            @Override
            public void onNext(CommonResponse<List<DriverInfo>> commonResponse) {
                if (!isFinishing() && !isDestroyed()) {
                    if (commonResponse.getCode() == 20000) {
                        List<DriverInfo> list = commonResponse.getData();
                        List<DriverInfo> resultList = new ArrayList<>();

                        if (list != null && list.size() > 0) {
                            setPageInfo(1, 1);
                            for (DriverInfo info : list) {
                                if (info.drivertel.equals(phone)) {
                                    resultList.add(info);
                                }
                            }
                            if (list.size() > 0) {
                                showDataList(list);
                            } else {
                                showEmptyView();
                            }

                        } else {
                            showEmptyView();
                        }

                    } else {
                        showToast(commonResponse.getMsg());
                    }
                }
            }


            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }
}